import { Component, Input, OnInit } from '@angular/core';

import { ModalController } from '@ionic/angular';

import { HttpService } from 'src/app/shared/service/http.service';
import { ImgSlidePage } from 'src/app/coms/slides-imgage/img-slide.page';

import { AppGlobalService } from '../../shared/service/app-global.service';
@Component({
  selector: 'app-promotion-record-list',
  templateUrl: './promotion-record-list.component.html',
  styleUrls: ['./promotion-record-list.component.scss'],
})
export class PromotionRecordListComponent implements OnInit {

  @Input() tagData: any;
  defaultImg = 'assets/img/default_img.png';
  sessionId: string;

  // 筛选及页面数据参数
  params: any = {};

  // 当前页数，默认1
  pageNo = 1;

  // size
  pageSize: number;

  // 数量
  pageTotal: number;

  listData: any;
  a: string;
  constructor(
    private appGlobalService: AppGlobalService,
    private http: HttpService,
    private modalCtrl: ModalController
  ) {
  }
  ngOnInit() {
    // console.log('msg----->' + JSON.stringify(this.tagData));
    // if (this.tagData.data.data.length === 0 && this.tagData.data.submit.content.search) {
    //   this.a = eval('(' + this.tagData.data.submit.content.search + ')').opportunity_title;
    // }
    this.sessionId = localStorage.getItem('sessionId');
    if (this.tagData.data.submit !== undefined) {
      // 初始化参数
      this.params = this.tagData.data.submit;
      this.params.sessionId = this.sessionId;
      this.listData = this.tagData.data.data;
      this.pageSize = this.tagData.pageSize;
      this.pageTotal = this.tagData.data.pageTotal;
    }
    // if (!this.subscription && this.tagData.data.submit !== undefined) {
    //   // 获取筛选参数
    //   this.subscription = this.message.getMessage().subscribe(param => {
    //     // // 重置当前页数
    //     this.pageNo = 1;
    //     // 参数
    //     this.params.content = param;
    //     // 页面数据参数
    //     this.params.content.pageNo = this.pageNo;
    //     // 请求获取新数据
    //     this.http.post('application/submit', this.params, res => {
    //       // 请求数据成功
    //       if (res.success) {
    //         const result = res.data.data; // JSON.parse(res.data.data);
    //         // 筛选数据
    //         this.tagData.data.data = result;
    //       } else {
    //         // 请求数据失败
    //         this.appGlobalService.showToast(res.message, 2000, 'middle');
    //       }
    //     },
    //       err => {
    //         // 请求数据失败
    //         this.appGlobalService.showToast(err.message, 2000, 'middle');
    //       });
    //   });
    // }
    // const len = this.tagData.data.data.length;
    // if (len > 0) {
    //   for (let i = 0; i < len; i++) {
    //     const v = this.tagData.data.data[i];
    //     if (v.images && v.images.imageUrl !== undefined && (v.images.imageUrl + '').startsWith('http')) {
    //       let url = v.images.imageUrl ;
    //       url = url.replace(';', '');
    //       v.images.imageUrl = url;
    //     } else if (v.images !== undefined) {
    //       v.images = { imageUrl: this.defaultImg };
    //     }
    //     this.tagData.data.data[i] = v;
    //   }
    // }
  }
  // ngOnDestroy(): void {
  //   if (this.tagData.data.submit !== undefined) {
  //     // 组件生命周期结束的时候，注销
  //     this.subscription.unsubscribe();
  //   }
  // }
  clickLink(obj) {
    if (!obj) {
      console.log('没有返回link-target');
    }
    const pageParams = {
      pageId: obj.target,
      params: obj.params,
      sessionId: localStorage.getItem('sessionId')
    };
    this.appGlobalService.goToForward(pageParams);
  }

  loadMoreData(event) {
    if (this.pageNo >= this.pageTotal) {
      event.target.disabled = true;
      // this.appGlobalService.showToast('没有数据了...', 1000, 'middle');
      // event.complete();
      return;
    }
    // // 请求下一个页面
    this.pageNo = this.pageNo + 1;
    // // 参数
    this.params.content.pageNo = this.pageNo;
    // 请求获取新数据
    this.http.post('application/submit', this.params, res => {

      // 请求数据成功
      if (res.success) {
        const resList = res.data.data;
        // 追加返回数据
        if (resList && resList.length > 0) {
          // 追加数据
          resList.map(v => {
            // if (v.images && v.images.imageUrl !== undefined && (v.images.imageUrl + '').startsWith('http')) {
            //   let url = v.images.imageUrl ;
            //   url = url.replace(';', '');
            //   v.images.imageUrl = url;
            // } else if (v.images !== undefined) {
            //   v.images = { imageUrl: this.defaultImg };
            // }
            this.tagData.data.data.push(v);
          });
        } else {
          event.target.disabled = true;
          // 没有数据了
          // this.appGlobalService.showToast('没有数据了...', 1000, 'middle');
        }
      } else {
        // 请求失败，更新当前页数
        this.pageNo = this.pageNo - 1;
        // 请求数据失败
        this.appGlobalService.showToast(res.message, 2000, 'middle');
      }
      event.target.complete();
    }, err => {
      // 请求失败，更新当前页数
      this.pageNo = this.pageNo - 1;
      // 请求数据失败
      this.appGlobalService.showToast(err.message, 2000, 'middle');
      event.target.complete();
    });
  }
  doRefresh(event) {
  }
  get(obj) {
    const params = {
      pageId: obj.target,
      params: obj.params,
      sessionId: this.sessionId
    };
    this.appGlobalService.goToForward(params);
  }
  gotoUrl(obj) {
    if (!obj) {
      console.log('没有返回link-target');
    }
    if (obj.type === 'url') {
    /*   console.log(obj.target)
      let urlparams='https://'+obj.target;
      console.log(urlparams) */
      this.appGlobalService.openUrl('',obj.target);
      /* this.appGlobalService.openUrl('','http://www.pupuwang.com/h5/aptitude') */
      // const secDiv = document.getElementById('protocol-section');
      // const iframe = document.getElementById("iframe-new-url");
      // secDiv.style.display = "none";
      // iframe.style.display = "block";
      // iframe.setAttribute("src", obj.target); // obj.target
    } else {
      this.get(obj);
    }
  }

  // 打开图片展示框，滑动展示
  async openImg(urls) {
    const value = { img: urls, index: 0 };
    const modal = await this.modalCtrl.create({
      component: ImgSlidePage,
      componentProps: { value: JSON.stringify(value) }
    });
    return await modal.present();
  }
}
